package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.AdminCategoryService;
import com.cskaoyan.market.db.admin.vo.CategoryIVo;
import com.cskaoyan.market.db.admin.vo.CategoryInnerCategoryVo;
import com.cskaoyan.market.db.admin.vo.MyCategoryVo;
import com.cskaoyan.market.db.admin.vo.CategorysVo;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.util.MarketCategoryConstant;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author tangle
 * @since 2023/03/22 09:43
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Override
    public MyCategoryVo list() {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        List<MarketCategory> marketCategory = categoryMapper.selectByExampleSelective(new MarketCategoryExample());
        session.commit();
        session.close();
        List<CategoryInnerCategoryVo> list = new ArrayList<>();
        MyCategoryVo categoryVo = new MyCategoryVo();
        CategoryInnerCategoryVo innerMyCategoryVo = new CategoryInnerCategoryVo();
        for (MarketCategory category : marketCategory) {
            if (category.getLevel().equals("L1")) {
                innerMyCategoryVo = new CategoryInnerCategoryVo();
                innerMyCategoryVo.setLabel(category.getName());
                innerMyCategoryVo.setValue(category.getId());
                list.add(innerMyCategoryVo);
            }
        }
        categoryVo.setList(list);
        return categoryVo;
    }

    @Override
    public CategoryIVo innerList() {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        List<MarketCategory> marketCategory = categoryMapper.selectByExampleSelective(new MarketCategoryExample());
        session.commit();
        session.close();
        Map<String, List<MarketCategory>> categoryListByLevel = groupByLevel(marketCategory);

        List<MarketCategory> fatherList = categoryListByLevel.get(MarketCategoryConstant.MARKET_LEVEL_FATHER);
        List<MarketCategory> childrenList = categoryListByLevel.get(MarketCategoryConstant.MARKET_LEVEL_CHILDREN);

        Map<Integer, List<MarketCategory>> childrenListGroup = groupByPid(childrenList);
        List<CategorysVo> categoryVos = new ArrayList<>();
        for (MarketCategory father : fatherList) {
            CategorysVo category = new CategorysVo();
            category.setId(father.getId());
            category.setName(father.getName());
            category.setDesc(father.getDesc());
            category.setIconUrl(father.getIconUrl());
            category.setKeywords(father.getKeywords());
            category.setPicUrl(father.getPicUrl());
            category.setLevel(father.getLevel());

            List<MarketCategory> childrens = childrenListGroup.get(father.getId());
            List<CategorysVo> categoryVoChildren = new ArrayList<>();
            if (childrens != null) {
                for (MarketCategory childrenLists : childrens) {
                    CategorysVo category2 = new CategorysVo();
                    category2.setId(childrenLists.getId());
                    category2.setName(childrenLists.getName());
                    category2.setDesc(childrenLists.getDesc());
                    category2.setIconUrl(childrenLists.getIconUrl());
                    category2.setPicUrl(childrenLists.getPicUrl());
                    category2.setKeywords(childrenLists.getKeywords());
                    category2.setLevel(childrenLists.getLevel());
                    categoryVoChildren.add(category2);
                }
            }
            category.setChildren(categoryVoChildren);
            categoryVos.add(category);
        }
        CategoryIVo categoryIVo = new CategoryIVo();
        categoryIVo.setList(categoryVos);
        return categoryIVo;
    }

    /**
     * 根据父id 进行分类
     *
     * @param categrotyList
     * @return
     */
    private Map<Integer, List<MarketCategory>> groupByPid(List<MarketCategory> categrotyList) {
        Map<Integer, List<MarketCategory>> result = new HashMap<>();
        // 遍历 categrotys 集合
        for (MarketCategory categroty : categrotyList) {
            // 根据pid类型分类，0,id将list加以分类
            // 通过类型获取categrotyList
            List<MarketCategory> categrotys = result.get(categroty.getPid());
            // 如果categrotyList == null
            if (categrotys == null) {
                // 新建一个list
                categrotys = new ArrayList<>();
                // 添加进来
                result.put(categroty.getPid(), categrotys);
            }
            // 将这个categroty对象添加进来
            categrotys.add(categroty);
        }
        return result;
    }

    /**
     * 根据L1,L2类型进行分组
     *
     * @param MarketCategorys
     * @return
     */
    private Map<String, List<MarketCategory>> groupByLevel(List<MarketCategory> MarketCategorys) {
        Map<String, List<MarketCategory>> result = new HashMap<>();
        // 遍历 categrotys 集合
        for (MarketCategory MarketCategory : MarketCategorys) {
            // 根据Level类型分类，L1,L2
            // 通过类型获取categrotyList
            String level = MarketCategory.getLevel();
            List<MarketCategory> categrotyList = result.get(level);
            // 如果categrotyList == null
            if (categrotyList == null) {
                // 新建一个list
                categrotyList = new ArrayList<>();
                // 添加进来
                result.put(MarketCategory.getLevel(), categrotyList);
            }
            // 将这个categroty对象添加进来
            categrotyList.add(MarketCategory);
        }
        return result;
    }

    @Override
    public void insertCategory(String desc, String name, String iconUrl, String keywords, String level,
        LocalDateTime addtime, LocalDateTime updatetime, Integer pid, String picUrl, Integer id) {
        // 获取sqlSession
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setName(name);
        marketCategory.setIconUrl(iconUrl);
        marketCategory.setDesc(desc);
        marketCategory.setKeywords(keywords);
        marketCategory.setLevel(level);
        if(addtime==null){
            addtime=LocalDateTime.now ();
        }
        marketCategory.setAddTime(addtime);
        if(updatetime==null){
            updatetime=LocalDateTime.now ();
        }
        marketCategory.setUpdateTime(updatetime);
        marketCategory.setId(id);
        marketCategory.setPicUrl(picUrl);
        marketCategory.setPid(pid);
        categoryMapper.insertSelective(marketCategory);
        session.commit();
        session.close();
    }

    @Override
    public void updateCategory(String desc, String name, String iconUrl, String keywords, String level,
        LocalDateTime addtime, LocalDateTime updatetime, Integer pid, String picUrl, Integer id) {
        // 获取sqlSession
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        // MarketCategoryExample categoryExample = new MarketCategoryExample ();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setName(name);
        marketCategory.setIconUrl(iconUrl);
        marketCategory.setDesc(desc);
        marketCategory.setKeywords(keywords);
        marketCategory.setLevel(level);
        if(addtime==null){
            addtime=LocalDateTime.now ();
        }
        marketCategory.setAddTime(addtime);
        if(updatetime==null){
            updatetime=LocalDateTime.now ();
        }
        LocalDateTime now = LocalDateTime.now ( );
        marketCategory.setUpdateTime(now);
        marketCategory.setId(id);
        marketCategory.setPicUrl(picUrl);
        marketCategory.setPid(pid);
        try {
            categoryMapper.updateByPrimaryKeySelective ( marketCategory );
            session.commit();
        } catch (Exception e) {
            e.printStackTrace ( );
            session.rollback ();
        } finally {
            session.close();
        }
    }

    @Override
    public void deleteCategory(Integer id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(id);
        categoryMapper.deleteByPrimaryKey (id);
        session.commit();
        session.close();
    }
}
